"use strict";


/**
 * 领域词语捕获
 * @constructor
 */
function BaseCapturer(dictName, isDebug) {
  this.dictName = dictName;
  this.__isDebug = !!isDebug;
  var vocabulary = require('./' + dictName + '.json');
  this.init(vocabulary.keywordList, vocabulary.referList);
}
module.exports = BaseCapturer;

BaseCapturer.prototype.init = function (keywordList, referList) {
  this.keywordList = keywordList;
  this.referList = referList;
};
module.exports = BaseCapturer;

/**
 * 对外接口,保持不变
 * @param textAndFreq {object} { "旅游":3, "吃饭":4}
 * @returns {string} keyword | refer | none
 */
BaseCapturer.prototype.capture = function (textAndFreq) {
  var self = this;
  self.__keyword = {};
  self.__refer = {};

  for (var key of Object.keys(textAndFreq)) {
    self.__innerCapture(key, Number(textAndFreq[key] || 1));
  }
};

/**
 * 通用捕获规则
 * @param str
 * @returns {string}
 */
BaseCapturer.prototype.__innerCapture = function (str, freq) {
  if (this.keywordList && this.keywordList[str] !== undefined) {
    this.__keyword[str] = 'freq=' + freq + ', score=' + this.keywordList[str];
  }
  if (this.referList && this.referList[str] !== undefined) {
    this.__refer[str] = 'freq=' + freq + ', score=' + this.referList[str];
  }
  // 其他策略
  return this.detailCapture(str);
};

/**
 * 通用过滤
 * @param str
 * @returns {string}
 */
BaseCapturer.prototype.detailCapture = function (str) {
  // TODO
};


/**
 * 查看过滤细节,并进行清理(debug mode)
 */
BaseCapturer.prototype.showAndClean = function () {
  if (!this.__isDebug) {
    console.log('BaseFilter not in debug mode');
    return;
  }
  console.log('----' + this.constructor.name);
  console.log(' keyword:', this.__keyword);
  this.__passed = [];
  console.log(' refer:', this.__refer);
  this.__blocked = [];
};


/**
 * 测试脚本
 * @param sampleObj
 * @private
 */
BaseCapturer.prototype.__testScript = function (sampleObj) {
  var self = this;
  self.capture(sampleObj);
  console.log('sample:', sampleObj);
  self.showAndClean();
};

// test
//var sample = {"旅游": 1, "旅途": 2, "旅毛": 3, "离开": 4, "说走就走": 5};
//new BaseCapturer('wordTour', true).__testScript(sample);